package com.ikkong.api;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.ikkong.api.util.TokenUtil;
import com.ikkong.common.vo.User;
import com.ikkong.core.constant.ConstCache;
import com.ikkong.platform.model.Member;
import com.jfinal.plugin.ehcache.CacheKit;


public class TokenManager {
    private static TokenManager me = new TokenManager();
    
    private Map<String, Member> tokens;
    private Map<String, String> userToken;

    public TokenManager() {
        tokens = new ConcurrentHashMap<String, Member>();
       userToken = new ConcurrentHashMap<String, String>();
    }

   /**
     * 获取单例对象
     * @return
     */
    public static TokenManager getMe() {
        return me;
    }

    /**
     * 验证token
     * @param token
     * @return
     */
    public Member validate(String token) {
        return CacheKit.get(ConstCache.USER_CACHE, token);
    }

    /**
     * 生成token值
     * @param user
     * @return
     */
    public String generateToken(Member user) {
        String token = TokenUtil.generateToken();
        //userToken.put(user.getId().toString(), token);
        CacheKit.put(ConstCache.USER_CACHE, token, user);
        //tokens.put(token, user);
        
        return token;
    }
}